package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.ilearn.bean.StudyContent;
import com.jnu.ilearn.bean.StudyTime;
import com.jnu.ilearn.utils.DataLoader;
import com.jnu.ilearn.utils.SeatUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SitDownActivity extends AppCompatActivity {

    //获取用户选择的自习内容、时间
    String studyContentString;
    Calendar studyTime;

    private GridView contentGridview;
    private GridView timeGridview;
    private ContentItemAdapter contentItemAdapter;
    private TimeItemAdapter timeItemAdapter;
    private List<StudyContent> studyContentList=new ArrayList<>();
    private List<StudyTime> studyTimeList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sit_down);

        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //初始化数据
        initStudyContent();
        initStudyTime();

        //将适配器上的数据传给studyContentList
        contentGridview =(GridView) findViewById(R.id.set_content);
        contentItemAdapter=new ContentItemAdapter(SitDownActivity.this,R.layout.study_content_item,studyContentList);
        contentGridview.setAdapter(contentItemAdapter);
        contentGridview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                for(int i=0;i<6;i++){
                    studyContentList.get(i).setHit(0);
                }
                studyContentList.get(position).setHit(1);
                studyContentString=studyContentList.get(position).getStudy_content();
                contentItemAdapter.notifyDataSetChanged();
            }
        });

        //将适配器上的数据传给studyTimeList
        timeGridview =(GridView) findViewById(R.id.set_time);
        timeItemAdapter=new TimeItemAdapter(SitDownActivity.this,R.layout.study_time_item,studyTimeList);
        timeGridview.setAdapter(timeItemAdapter);
        timeGridview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if(studyTimeList.get(position).getStudyTime()=="马上开始"){
                    for(int i=0;i<2;i++){
                        studyTimeList.get(i).setHit(0);
                    }
                    studyTimeList.get(position).setHit(1);
                    timeItemAdapter.notifyDataSetChanged();
                    studyTime=Calendar.getInstance();
                }
                else if(studyTimeList.get(position).getStudyTime()=="五分钟后开始"){
                    for(int i=0;i<2;i++){
                        studyTimeList.get(i).setHit(0);
                    }
                    studyTimeList.get(position).setHit(1);
                    timeItemAdapter.notifyDataSetChanged();
                    studyTime=Calendar.getInstance();
                    int minute=studyTime.get(Calendar.MINUTE);
                    studyTime.set(Calendar.MINUTE,minute+5);
                }
            }
        });

        Intent intent=getIntent();
        int seatId=intent.getIntExtra("seatId",0);
        String user=intent.getStringExtra("user");
        int position=intent.getIntExtra("position",0);
        TextView enSure=findViewById(R.id.ensure_button);
        enSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(studyContentString!=null&&studyTime!=null){
                    //更改数据库信息
                    SeatUtils seatUtils=new SeatUtils();
                    Handler handler=new Handler(Looper.getMainLooper()){
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            if(seatUtils.isUse()){
                                //更改成功，返回主界面
                                Intent intent=new Intent();
                                intent.putExtra("position",position);
                                intent.putExtra("studyContent",studyContentString);
                                intent.putExtra("studyTime",studyTime);
                                setResult(1,intent);
                                finish();
                            }
                            else {
                                new AlertDialog.Builder(SitDownActivity.this).setTitle("提示").setMessage("占座失败！").show();
                            }
                        }
                    };
                    seatUtils.UseSeat(handler,seatId,studyContentString,studyTime,user);
                }
                else{
                    //设置提示信息
                    new AlertDialog.Builder(SitDownActivity.this).setTitle("警告").setMessage("学习内容或时间未选择！").show();
                }
            }
        });
        TextView cancel=findViewById(R.id.cancel_button);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回主界面
                finish();
            }
        });
    }


    private void initStudyContent(){
        StudyContent android =new StudyContent(R.drawable.android,"Android");
        StudyContent c =new StudyContent(R.drawable.c,"C++");
        StudyContent math =new StudyContent(R.drawable.math,"Math");
        StudyContent english =new StudyContent(R.drawable.english,"English");
        StudyContent java =new StudyContent(R.drawable.java,"Java");
        StudyContent php =new StudyContent(R.drawable.php,"PHP");
        studyContentList.add(android);
        studyContentList.add(c);
        studyContentList.add(math);
        studyContentList.add(english);
        studyContentList.add(java);
        studyContentList.add(php);
    }

    private void initStudyTime(){
        StudyTime studyTime_now=new StudyTime("马上开始");
        StudyTime studyTime_after=new StudyTime("五分钟后开始");
        studyTimeList.add(studyTime_now);
        studyTimeList.add(studyTime_after);
    }

    private class ContentItemAdapter extends ArrayAdapter<StudyContent> {
        private int resourceId;

        //适配器的构造函数，把要适配的数据传入这里
        public ContentItemAdapter(@NonNull Context context, int resource, @NonNull List<StudyContent> objects) {
            super(context, resource, objects);
            resourceId=resource;
        }

        //设置显示的图片
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            StudyContent studyContentItem=(StudyContent) getItem(position);
            View view;
            ContentItemAdapter.ViewHolder viewHolder;
            if(convertView==null){
                view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
                viewHolder=new ContentItemAdapter.ViewHolder();
                viewHolder.imageView=(ImageView) view.findViewById(R.id.study_content_image);
                viewHolder.textView=(TextView) view.findViewById(R.id.study_content_string);
                view.setTag(viewHolder);    // 将ViewHolder存储在View中
            }else{
                view=convertView;
                viewHolder = (ContentItemAdapter.ViewHolder) view.getTag(); // 重新获取ViewHolder
            }
            viewHolder.imageView.setImageResource(studyContentItem.getStudyContentImage());
            viewHolder.textView.setText(studyContentItem.getStudy_content());

            ColorStateList BackGroundColor=ColorStateList.valueOf(0xFFFFFFFD);
            ColorStateList hitColor=ColorStateList.valueOf(0xFFb3ecff);
            if(studyContentList.get(position).getHit()==1){
                viewHolder.imageView.setBackgroundColor(Color.parseColor("#72baa7"));
                viewHolder.textView.setBackgroundColor(Color.parseColor("#72baa7"));
            }
            else {
                viewHolder.imageView.setBackgroundColor(Color.parseColor("#FFFFFD"));
                viewHolder.textView.setBackgroundColor(Color.parseColor("#FFFFFD"));
            }
            return view;
        }

        class ViewHolder{
            ImageView imageView;
            TextView textView;
        }
    }

    private class TimeItemAdapter extends ArrayAdapter<StudyTime> {
        private int resourceId;

        //适配器的构造函数，把要适配的数据传入这里
        public TimeItemAdapter(@NonNull Context context, int resource, @NonNull List<StudyTime> objects) {
            super(context, resource, objects);
            resourceId=resource;
        }

        //设置显示的图片
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            StudyTime studyTimeItem=(StudyTime) getItem(position);
            View view;
            TimeItemAdapter.ViewHolder viewHolder;
            if(convertView==null){
                view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
                viewHolder=new TimeItemAdapter.ViewHolder();
                viewHolder.textView=(TextView) view.findViewById(R.id.study_time);
                view.setTag(viewHolder);    // 将ViewHolder存储在View中
            }else{
                view=convertView;
                viewHolder = (TimeItemAdapter.ViewHolder) view.getTag(); // 重新获取ViewHolder
            }
            viewHolder.textView.setText(studyTimeItem.getStudyTime());

            if(studyTimeList.get(position).getHit()==1){
                viewHolder.textView.setBackgroundColor(Color.parseColor("#72baa7"));
            }
            else {
                viewHolder.textView.setBackgroundColor(Color.parseColor("#FFFFFD"));
            }
            return view;
        }

        class ViewHolder{
            TextView textView;
        }
    }
}